package com.ly.algorithm.DataStructures.linkedlist;
import	java.util.Stack;

/**
 * @Classname SingleLinkedList
 * @Description TODO
 * @Date 2020/4/21 10:13
 * @Author 冷心影翼
 */
public class SingleLinkedList {

    //头节点
    private HeroNode head;

    public SingleLinkedList() {
        head = new HeroNode(0,"","");
    }

    public void add(HeroNode heroNode) {
        //操作节点
        HeroNode temp = head;
        while(true) {
            //判断为空
            if(temp.next == null) {
                break;
            }
            //操作节点下移
            temp = temp.next;
        }
        //最后一个节点赋值
        temp.next = heroNode;
    }

    public void addByOrder(HeroNode heroNode) {
        HeroNode temp = head;

        boolean flag = false;
        while(true) {
            if(temp.next == null) {
                break;
            }
            //插入的时候  temp 应该在    next节点的前面
            if(temp.next.no>heroNode.no) {
                break;
            }
            //是否有值
            if(temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            System.out.println("编号被占用了");
            return;
        }
        heroNode.next = temp.next;
        temp.next = heroNode;
    }

    public void update(HeroNode heroNode) {
        HeroNode temp = head;
        if(temp.next == null) {
            System.out.println("链表为空");
            return;
        }
        while(true) {
            if(temp.next == null) {
                System.out.println("没有找到数据");
                break;
            }
            //更新时候   temp  应该在  需要更新节点的前面
            if(temp.next.no == heroNode.no) {
                temp.next.name = heroNode.name;
                temp.next.nickname = heroNode.nickname;
                break;
            }
            temp = temp.next;
        }
    }

    public void delete(int no) {
        HeroNode temp = head;

        if(temp.next == null) {
            System.out.println("链表为空");
        }
        while (true) {
            if(temp.next == null) {
                System.out.println("没有找到数据");
                return;
            }
            //删除  temp  在 删除节点的前面  把next  指向 删除节点的next
            if(temp.next.no == no) {
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
    }

    public void list() {
        if(head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode heroNode = head.next;
        while(true) {
            if(heroNode==null) {
                break;
            }
            System.out.println(heroNode);
            heroNode = heroNode.next;
        }
    }

    public int getSize() {
        if(head.next == null) {
            return 0;
        }
        HeroNode temp = head;
        int size = 0;
        while (true) {
            if(temp.next==null) {
                break;
            }
            temp = temp.next;
            size ++;
        }
        return size;
    }

    public HeroNode get(int index) {
        HeroNode temp = head;
        if(index<=0 || index>getSize()) {
            return null;
        }
        int count = 0;
        while(true) {
            if(count == index) {
                return temp;
            }
            if (temp.next == null) {
                System.out.println("链表为空");
            }
            temp = temp.next;
            count++;
        }
    }

    public void reverse() {
        if(head.next == null || head.next.next == null) {
            return ;
        }
        HeroNode reverseHead = new HeroNode(0,"","");
        HeroNode cur = head.next;
        HeroNode next = null;

        while(cur!=null) {
           //记录  当前节点  下一个节点
           next = cur.next;
           //当前节点 = 上一个节点
           cur.next = reverseHead.next;
           //head 的next  = 当然节点
           reverseHead.next = cur;

           cur = next;
        }
        head.next= reverseHead.next;
    }

    public void listReverse() {
        if(head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        Stack <HeroNode> stack = new Stack<>();
        while(true) {
            if(temp == null) {
                break;
            }
            stack.push(temp);
            temp = temp.next;
        }
        while (stack.size()>0) {
            System.out.println(stack.pop());
        }
    }
}

class Test {
    public static void main(String[] args) {
        SingleLinkedList linked = new SingleLinkedList();
        linked.add(new HeroNode(1,"a","a"));
        linked.add(new HeroNode(22,"b","b"));
        linked.addByOrder(new HeroNode(12,"c","c"));
        linked.update(new HeroNode(12,"c","d"));
        linked.update(new HeroNode(15,"c","d"));
//        linked.delete(1.txt);
//        linked.delete(22);
        linked.reverse();
//        System.out.println(linked.getSize());
//        System.out.println(linked.get(linked.getSize()-1.txt+1.txt));
        linked.list();
        System.out.println("-----");
        linked.listReverse();
    }
}

class HeroNode {

    public int no;

    public String name;

    public String nickname;

    public HeroNode next;

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", number='" + nickname + '\''
               ;
    }

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }
}
